package com.gnerv.battle.boot.module.base.service.impl;

import com.gnerv.battle.boot.common.tools.DateTimeTools;
import com.gnerv.battle.boot.module.base.repository.mapper.AuditApiMapper;
import com.gnerv.battle.boot.module.base.model.dto.AuditApiDTO;
import com.gnerv.battle.boot.module.base.model.entity.AuditApi;
import com.gnerv.battle.boot.module.base.service.AuditApiService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.gnerv.battle.boot.common.tools.PageTools.limitSize;
import static com.gnerv.battle.boot.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class AuditApiServiceImpl implements AuditApiService {

    private final AuditApiMapper auditApiMapper;

    @Override
    @Async(value = "struggleExecutor")
    public void create(AuditApi auditApi) {
        auditApi.setGmtCreate(DateTimeTools.currentTimeMillis());
        auditApi.setGmtModified(DateTimeTools.currentTimeMillis());
        auditApiMapper.insert(auditApi);
    }

    @Override
    public AuditApiDTO selectByUkId(String ukId) {
        return auditApiMapper.selectByUkId(ukId);
    }

    @Override
    @Async(value = "struggleExecutor")
    public void updateStatusByUkId(String ukId, String status) {
        auditApiMapper.updateStatusByUkId(ukId, status);
    }

    @Override
    @Async(value = "struggleExecutor")
    public void updateByUkId(AuditApi auditApi) {
        auditApiMapper.updateByUkId(auditApi);
    }

    @Override
    public List<AuditApiDTO> listByName(String name, Integer page, Integer size) {
        return auditApiMapper.listByName(name, limitStart(page, size), limitSize(size));
    }

    @Override
    public int countByName(String name) {
        return auditApiMapper.countByName(name);
    }
}
